package com.yida.gwj.finance.controller;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.yida.common.core.domain.R;
import com.yida.common.core.enums.UserStatus;
import com.yida.common.core.utils.StringUtils;
import com.yida.common.core.utils.poi.ExcelUtil;
import com.yida.common.core.web.controller.BaseController;
import com.yida.common.core.web.domain.AjaxResult;
import com.yida.common.core.web.page.TableDataInfo;
import com.yida.common.log.annotation.Log;
import com.yida.common.log.enums.BusinessType;
import com.yida.common.security.annotation.InnerAuth;
import com.yida.common.security.annotation.RequiresPermissions;
import com.yida.gwj.api.domain.GwjFinanceBank;
import com.yida.gwj.finance.service.IGwjFinanceBankService;
import com.yida.gwj.finance.service.IGwjFinanceMoneyService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @author lqq
 * @date 2022-12-30
 */
@Api(tags = "银行基础数据设置")
@RestController
@RequestMapping("/bank")
public class GwjFinanceBankController extends BaseController {

    @Resource
    private IGwjFinanceBankService financeBankService;

    @Resource
    private IGwjFinanceMoneyService financeMoneyService;

    @ApiOperation("查询银行信息列表")
    @GetMapping("/list")
    @RequiresPermissions("gwj:finance:bank:list")
    public TableDataInfo list(GwjFinanceBank financeBank) {
        // 查询未删除的
        financeBank.setDelFlag(UserStatus.OK.getCode());
        startPage();
        List<GwjFinanceBank> list = financeBankService.selectFinanceBankList(financeBank);
        if (StringUtils.isNotEmpty(list)) {
            list.forEach(f -> f.setDepositMoney(financeMoneyService.countDepositMoney(f.getBankId(), f.getAreaId())));
        }
        return getDataTable(list);
    }

    @ApiOperation("查询所有银行")
    @GetMapping("/list/all")
    public AjaxResult listAll(GwjFinanceBank financeBank) {
        List<GwjFinanceBank> list = financeBankService.selectFinanceBankList(financeBank);
        return AjaxResult.success(list);
    }

    @ApiOperation("查询未删除所有银行")
    @GetMapping("/list/allValid")
    public AjaxResult listAllValid(GwjFinanceBank financeBank) {
        financeBank.setDelFlag(UserStatus.OK.getCode());
        List<GwjFinanceBank> list = financeBankService.selectFinanceBankList(financeBank);
        return AjaxResult.success(list);
    }

    @ApiOperation("导出银行信息列表")
    @PostMapping("/export")
    @RequiresPermissions("gwj:finance:bank:export")
    @Log(title = "银行信息", businessType = BusinessType.EXPORT, systemName = "gwj")
    public void export(HttpServletResponse response, GwjFinanceBank financeBank) {
        // 查询未删除的
        financeBank.setDelFlag(UserStatus.OK.getCode());
        List<GwjFinanceBank> list = financeBankService.selectFinanceBankList(financeBank);
        ExcelUtil<GwjFinanceBank> util = new ExcelUtil<GwjFinanceBank>(GwjFinanceBank.class);
        util.exportExcel(response, list, "银行信息数据");
    }

    @ApiOperation("获取银行信息详细信息")
    @GetMapping(value = "/{id}")
    @RequiresPermissions("gwj:finance:bank:query")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(financeBankService.selectFinanceBankById(id));
    }

    @ApiOperation("新增银行信息")
    @PostMapping
    @RequiresPermissions("gwj:finance:bank:add")
    @Log(title = "银行信息", businessType = BusinessType.INSERT, systemName = "gwj")
    public AjaxResult add(@Validated @RequestBody GwjFinanceBank financeBank) {
        return toAjax(financeBankService.insertFinanceBank(financeBank));
    }

    @ApiOperation("修改银行信息")
    @PutMapping
    @RequiresPermissions("gwj:finance:bank:edit")
    @Log(title = "银行信息", businessType = BusinessType.UPDATE, systemName = "gwj")
    public AjaxResult edit(@Validated @RequestBody GwjFinanceBank financeBank) {
        return toAjax(financeBankService.updateFinanceBank(financeBank));
    }

    @ApiOperation("删除银行信息")
    @DeleteMapping("/{ids}")
    @RequiresPermissions("gwj:finance:bank:remove")
    @Log(title = "银行信息", businessType = BusinessType.DELETE, systemName = "gwj")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(financeBankService.deleteFinanceBankByIds(ids));
    }

    /**
     * 获取银行信息详细信息
     */
    @InnerAuth
    @GetMapping("/{bankId}/{areaId}")
    public R<GwjFinanceBank> selectFinanceBankById(@PathVariable("bankId") Long bankId, @PathVariable("areaId") Long areaId) {
        return R.ok(financeBankService.selectFinanceBankByBankIdAndAreaId(bankId, areaId));
    }

    /**
     * 查询银行Map
     * 
     * @param areaId
     * @return
     */
    @GetMapping("/selectFinanceBankMap/{areaId}")
    @ApiOperation("查询银行Map")
    public AjaxResult selectFinanceBankMap(@PathVariable("areaId") Long areaId) {
        GwjFinanceBank bank = new GwjFinanceBank();
        bank.setAreaId(areaId);
        return AjaxResult.success(financeBankService.selectFinanceBankList(bank).stream().collect(Collectors.toMap(GwjFinanceBank::getBankId, GwjFinanceBank::getBankName)));
    }

    /**
     * 20230710 新增 hmh
     * 
     * 查询银行Map
     * 
     * @param areaId
     * @return
     */
    @InnerAuth
    @GetMapping(value = "/selectFinanceBankMapInner/{areaId}")
    @ApiOperation("查询银行Map")
    public R<Map<Long, String>> selectFinanceBankMapInner(@PathVariable("areaId") Long areaId) {
        GwjFinanceBank bank = new GwjFinanceBank();
        bank.setAreaId(areaId);
        Map<Long, String> resultMap = financeBankService.selectFinanceBankList(bank).stream().collect(Collectors.toMap(GwjFinanceBank::getBankId, GwjFinanceBank::getBankName));
        return R.ok(resultMap);
    }
}
